package com.SouthernWall_404.AgeRight.Common.Event;

import com.SouthernWall_404.AgeRight.Common.API.*;
import com.SouthernWall_404.AgeRight.Common.API.LockAPI.KeyInfo;
import com.SouthernWall_404.AgeRight.Common.API.LockAPI.LockAPI;
import com.SouthernWall_404.AgeRight.Common.API.LockAPI.LockableBlocksRegistry;
import com.SouthernWall_404.AgeRight.Common.AgeRight;
import com.SouthernWall_404.AgeRight.Common.Init.ModItems;
import com.SouthernWall_404.AgeRight.Common.world.items.KeyItem;
import com.SouthernWall_404.AgeRight.Common.world.items.LockItem;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.SubscribeEvent;

import java.util.UUID;

public class onRightClick {

    public static final String hasLock= Components.LOCK+"haslock";
    public static final String lockSuccess = Components.LOCK+"lock_success";
    public static final String isLocked=Components.LOCK+"islocked";
    public static final String unLocked=Components.LOCK+"unlocked";

    @SubscribeEvent
    public static void onRightClickItem(PlayerInteractEvent.RightClickBlock event)
    {

        Player player=event.getEntity();


        InteractionHand hand=event.getHand();

        ItemStack itemStack=player.getItemInHand(hand);

        BlockPos blockPos=event.getPos();
        Level level=player.level();
        BlockState blockState=level.getBlockState(blockPos);
        if(blockState.getBlock()==Blocks.SMITHING_TABLE)
        {
            ItemStack itemStack1=player.getItemInHand(InteractionHand.MAIN_HAND);
            ItemStack itemStack2=player.getItemInHand(InteractionHand.OFF_HAND);
            if(itemStack2.getItem()instanceof KeyItem&&itemStack1.getItem()instanceof KeyItem)
            {
                if(itemStack1.getOrCreateTag().contains(AgeRight.MODID)&&(!itemStack2.getOrCreateTag().contains(AgeRight.MODID)))
                {
                    player.setItemInHand(InteractionHand.OFF_HAND,itemStack1.copy());
                    player.swing(InteractionHand.OFF_HAND);

                    event.setCanceled(true);
                    event.setCancellationResult(InteractionResult.FAIL);
                    event.setResult(Event.Result.DENY);
                    event.setUseItem(Event.Result.DENY);
                    event.setUseBlock(Event.Result.DENY);
                    return;
                }
            }
        }

        if(LockableBlocksRegistry.isLockable(blockState))
        {

            if(itemStack.getItem() instanceof LockItem)//手持锁头
            {
                if(player.isShiftKeyDown())//若玩家加了shift按键
                {
                    if(LockAPI.hasLock(blockPos,level))//若此处已经存在锁
                    {

                        player.displayClientMessage(Component.translatable(hasLock).withStyle(ChatFormatting.DARK_RED),true);//否决

                        return;//停止
                    }
                    else//若此处没有已存的锁
                    {
                        UUID id=UUID.randomUUID();//获取uuid
                        LockAPI.addLock(id,blockPos,level);//添加锁
                        ItemStack key= KeyItem.getKey(new KeyInfo(blockPos,blockState.getBlock().getDescriptionId(),id));//获取钥匙
                        player.setItemInHand(hand,key);//交给玩家

                        player.displayClientMessage(Component.translatable(lockSuccess).withStyle(ChatFormatting.GREEN),true);//说明锁住

                        event.setCanceled(true);
                        event.setCancellationResult(InteractionResult.FAIL);
                        event.setResult(Event.Result.DENY);
                        event.setUseItem(Event.Result.DENY);
                        event.setUseBlock(Event.Result.DENY);
                        return;//停止
                    }
                } else {//若只是简单右键，视为试图开锁
                    if(LockAPI.hasLock(blockPos,level))//如果有锁
                    {
                        event.setUseBlock(Event.Result.DENY);//正常执行尝试打开逻辑
                        player.displayClientMessage(Component.translatable(isLocked).withStyle(ChatFormatting.DARK_RED),true);
                    }
                }
            }
            else if(itemStack.getItem()instanceof KeyItem)//如果手持钥匙
            {
                if(LockAPI.hasLock(blockPos,level))//此处有锁
                {
                    KeyInfo keyInfo= KeyInfo.getFromItem(itemStack);
//                    player.sendSystemMessage(Component.literal("能否解锁:"+ LockAPI.isKeyTo(blockPos,level,keyInfo.uuid)));


                    if(LockAPI.isKeyTo(blockPos,level,keyInfo.uuid))//如果钥匙对应
                    {
                        if(player.isShiftKeyDown())//若玩家按下shift键
                        {
                            LockAPI.removeLock(blockPos,level);//解除上锁
                            player.setItemInHand(hand,new ItemStack(ModItems.LOCK.get(),1));//返还锁
                            player.displayClientMessage(Component.translatable(unLocked).withStyle(ChatFormatting.GREEN),true);


                            event.setCanceled(true);
                            event.setCancellationResult(InteractionResult.FAIL);
                            event.setResult(Event.Result.DENY);
                            event.setUseItem(Event.Result.DENY);
                            event.setUseBlock(Event.Result.DENY);
                            return;//取消后续操作
                        }
                        return;//没有按下shift键，正常打开
                    }else{//若钥匙不对应
                        event.setUseBlock(Event.Result.DENY);//拦截操作
                        player.displayClientMessage(Component.translatable(isLocked).withStyle(ChatFormatting.DARK_RED),true);

                    }
                }
            }else {//若正常打开
                if(LockAPI.hasLock(blockPos,level))//有锁
                {
                    if(player.getItemInHand(InteractionHand.MAIN_HAND).isEmpty())//如果玩家为管理员且创造模式
                    {
                        if(player.isCreative()&&player.hasPermissions(4))//主手空闲
                        {
                            if(player.isShiftKeyDown())//下蹲中
                            {

                                UUID id=LockAPI.getLockId(blockPos,level);//获取当前ID
                                ItemStack key= KeyItem.getKey(new KeyInfo(blockPos,blockState.getBlock().getDescriptionId(),id));//获取钥匙
                                player.setItemInHand(hand,key);//交给玩家

                                event.setCanceled(true);
                                event.setCancellationResult(InteractionResult.FAIL);
                                event.setResult(Event.Result.DENY);
                                event.setUseItem(Event.Result.DENY);
                                event.setUseBlock(Event.Result.DENY);
                                return;//拦截操作
                            }
                        }
                        else
                        {

                            UUID id=LockAPI.getLockId(blockPos,level);//获取当前ID
                            ItemStack key= KeyItem.getKey(new KeyInfo(blockPos,blockState.getBlock().getDescriptionId(),id));//获取钥匙
                            Inventory inventory=player.getInventory();
                            if(inventory.contains(key))
                            {
                                int i=inventory.findSlotMatchingItem(key);
                                inventory.setItem(i,ItemStack.EMPTY);
                                player.setItemInHand(InteractionHand.MAIN_HAND,key.copy());

                                event.setCanceled(true);
                                event.setCancellationResult(InteractionResult.FAIL);
                                event.setResult(Event.Result.DENY);
                                event.setUseItem(Event.Result.DENY);
                                event.setUseBlock(Event.Result.DENY);
                                return;//拦截操作
                            }
                        }
                    }
                    event.setUseBlock(Event.Result.DENY);//拦截
                    player.displayClientMessage(Component.translatable(isLocked).withStyle(ChatFormatting.DARK_RED),true);
                }
            }

        }
    }
}
